﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BonsaiScrum.Core.Query;
using BonsaiScrum.Core.Persistance;
using System.Security.Authentication;

namespace BonsaiScrum.Mvc.Services
{
    public class HttpUserSession : IUserSession
    {
        private readonly IUserReportingRepository _userReportingRepository;

        public HttpUserSession(IUserReportingRepository userReportingRepository)
        {
            _userReportingRepository = userReportingRepository;
        }

        public UserReport GetCurrentUser()
        {
            var identity = HttpContext.Current.User.Identity;

            if (identity.IsAuthenticated)
            {
                var currentUser = _userReportingRepository.GetUserWithUserName(identity.Name);
                return currentUser;
            }

            throw new AuthenticationException("There is no current user logged in!");
        }

        public int CurrentUserId
        {
            get { return GetCurrentUser().Id; }
        }
    }
}
